package oocl.ita.eordering.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import oocl.ita.eordering.po.Client;
import oocl.ita.eordering.po.Merchant;
import oocl.ita.eordering.po.Page;
import oocl.ita.eordering.po.ResponsePo;
import oocl.ita.eordering.service.ClientService;
import oocl.ita.eordering.service.OrderService;
import oocl.ita.eordering.util.PageUtil;
import oocl.ita.eordering.util.StringUtil;
import oocl.ita.eordering.vo.Vorder;
import oocl.ita.eordering.webService.GetOrderStatusWS;

@Controller
@RequestMapping("/homes")
public class OrderController {
	
	@Autowired
	private OrderService os;
	@Autowired
	private ClientService cs;
	
	
	//页面跳转：接单页面，加载未接单的订单数据（JQuery定时刷新）
		@RequestMapping("/pickUp")
		public ModelAndView pickUp(HttpServletRequest req){
			ModelAndView mov = new ModelAndView();
			Merchant m = (Merchant)req.getSession().getAttribute("user");
			List<Vorder> oList = os.findOrderByStatus(m.getmId(), 1);
			List<Vorder> rsList = new ArrayList<Vorder>();
			for(Vorder o: oList){
				Client temp = cs.findClient(o.getcId());
				o.setClient(temp);
				rsList.add(o);
			}
			mov.addObject("orders",rsList);
			mov.addObject("mainPage","pickOrders.jsp");
			mov.setViewName("home");
			return mov;
		}
		
		
		//按钮表单提交：接单成功后跳转到历史订单页面
		@RequestMapping("/pickOrder")
		public String pickOrder(Integer oid){
			os.acceptOrder(oid);
			return "redirect:/homes/listOrd";
		}
		
		//按钮表单提交：拒接订单，成功后跳转到历史订单页面
		@RequestMapping("/abandonOrder")
		public String abandonOrder(Integer oid){
			os.refuseOrder(oid);
			return "redirect:/homes/listOrd";
		}
		
		//页面跳转：历史订单页面，加载所有已接单的订单
		@RequestMapping("/listOrd")
		public ModelAndView listOrd(HttpServletRequest req){
			ModelAndView mov = new ModelAndView();
			Merchant m = (Merchant)req.getSession().getAttribute("user");
			
			String curPage = req.getParameter("curPage");
			if(StringUtil.isEmpty(curPage)){
				curPage="1";
			}
			Page p=new Page(Integer.parseInt(curPage),5);
			List<Vorder> oList = os.pageHistoryOrders(p, m.getmId());
			int total = os.countHistoryOrders(m.getmId());
			String pageCode = PageUtil.getPagation(req.getContextPath()+"/homes/listOrd?", total, Integer.parseInt(curPage), 5);
			
//			List<Vorder> oList = os.findHistoryOrders(m.getmId());
			List<Vorder> reList = new ArrayList<Vorder>();
			for(Vorder o: oList){
				Client temp = cs.findClient(o.getcId());
				o.setClient(temp);
				reList.add(o);
			}
			mov.addObject("orders",reList);
			mov.addObject("pageCode",pageCode);
			mov.addObject("mainPage","listOrders.jsp");
			mov.setViewName("home");
			return mov;
		}
		
		//页面跳转：投诉订单页面，加载所有有投诉信息的订单
		@RequestMapping("/compOrder")
		public ModelAndView compOrder(HttpServletRequest req){
			ModelAndView mov = new ModelAndView();
			Merchant m = (Merchant)req.getSession().getAttribute("user");
			List<Vorder> oList = os.findComplaintOrders(m.getmId());
			List<Vorder> reList = new ArrayList<Vorder>();
			for(Vorder o: oList){
				Client temp = cs.findClient(o.getcId());
				o.setClient(temp);
				reList.add(o);
			}
			mov.addObject("orders",reList);
			mov.addObject("mainPage","listComplainOrders.jsp");
			mov.setViewName("home");
			return mov;
		}
		
		//模态框弹出：投诉信息和处理结果状态
		@RequestMapping("/compResult")
		@ResponseBody
		public ResponsePo compResult(Integer oId){
			ResponsePo rs = new ResponsePo();
			Integer state = GetOrderStatusWS.getOrderStatus(oId);
			rs.setData(state);
			rs.setStatus(true);
			return rs;
		}

}
